<?php

/**
 * Sign up new user
 * 
 * @project : Loobica
 * @author  : Dino
 * @date    : 18 Jan 2014
 * 
 */
if (!defined('BASEPATH'))
  exit('No direct script access allowed');

class Signup extends LB_Controller {

  /**
   * Prevent signed in users
   * 
   */
  public function __construct() {
    parent::__construct();
    $this->prevent_signed();
  }

  /**
   * Sign up
   * 
   */
  public function index() {

    $user_email = null;

    // Processing user captcha security always for signup
    $user_security = $this->user_security->always()->run();

    if ($this->is_post()) {

      // Validate form
      $this->form_validation->set_rules('user_email', 'Email', 'required|valid_email|min_length[10]|max_length[50]|valid_unregisterd_email');
      $this->form_validation->set_rules('user_security_answer', 'Security answer', 'valid_security_answer');

      // Form success
      if ($this->form_validation->run()) {

        // Getting input values
        $user_email = (string) trim($this->input->post('user_email', true));

        // Send activation email
        $email_view_data = array();
        $email_view_data['signup_token'] = $this->encrypt->encode(json_encode(array('type' => 'signup', 'email' => $user_email)));
        $email_content = $this->load->view('signup/email', $email_view_data, true);
        $email_subject = lang('loobica_ads') . ' - ' . lang('sign_up');
        $this->send_email($user_email, $email_subject, $email_content);
      }
    }

    // Preparing view data
    $view_data = array();
    $view_data['title'] = lang('sign_up');
    $view_data['user_email'] = $user_email;
    $view_data['user_security'] = $user_security;

    // Loading view
    $this->load_view('signup/signup', $view_data);
    return;
  }

  /**
   * Sign up completion
   * 
   */
  public function complete() {

    // Init vars
    $user_password = null;
    $user_terms = null;
    $user_zipcode = null;

    // Getting token
    $signup_token = (string) trim($this->input->get('token', true));

    // Decrypting token
    $token_data = json_decode($this->encrypt->decode($signup_token), true);

    // Checking user data
    if (!empty($token_data) && ($token_data['type'] == 'signup') && (!empty($token_data['email']))) {

      // Check email already exists
      $get_email = $this->user_model->get_email($token_data['email']);

      // Confirm that email not already exists
      if (empty($get_email)) {

        // Form submit
        if ($this->is_post()) {

          // Validate form
          $this->form_validation->set_rules('user_password', 'password', 'required|min_length[5]|max_length[50]');
          $this->form_validation->set_rules('user_terms', 'terms & conditions', 'required');
          $this->form_validation->set_rules('user_zipcode', 'zip code', 'required|valid_zip_pin_code');

          // Form success
          if ($this->form_validation->run()) {

            // Getting input values
            $user_password = (string) $this->input->post('user_password', true);
            $user_terms = $this->input->post('user_terms', true);

            // Form validator will convert zip code to location key
            $user_location_key = $this->input->post('user_zipcode', true);

            // Get name from email
            $get_name = explode('@', $token_data['email']);

            // Preparing user data
            $user_data = array();
            $user_data['user_email'] = (string) strtolower($token_data['email']);
            $user_data['user_password'] = $this->loobica_hash($user_password);
            $user_data['user_display_name'] = (string) strtolower($get_name[0]);
            $user_data['user_location_key'] = $user_location_key;
            $user_data['user_create_date'] = date('Y-m-d H:i:s');
            $user_data['user_email_privacy'] = 1;
            $user_data['user_phone_privacy'] = 1;
            $user_data['user_social_privacy'] = 1;
            $user_data['user_type'] = 3;
            $user_data['user_status'] = 1;

            // Inserting user
            $user_id = (int) $this->user_model->put_user($user_data);

            // Sign in user
            $this->user_session->sign_on($user_id);

            // Redirect to edit profile
            $this->redirect('settings/profile');
            return;
          }
        }

        // Preparing and loading view
        $view_data = array();
        $view_data['title'] = lang('sign_up');
        $view_data['signup_token'] = $signup_token;
        $view_data['user_email'] = $token_data['email'];
        $view_data['user_password'] = $user_password;
        $view_data['user_zipcode'] = $user_zipcode;
        $view_data['user_terms'] = $user_terms;

        // Loading view
        $this->load_view('signup/complete', $view_data);
        return;
      }
    }

    // Redirect if something wrong
    $this->redirect('signup');
  }

}
